<template>
  <div>
    <!-- 菜单列表 应该是一个列表 进入页面自动发请求获取列表数据 -->
    <!-- data 表格的数据 父传子 -->
    <!-- row-key 选择元素节点的方式 -->
    <!-- default-expand-all 默认展开所有节点 -->
    <!-- tree-props 树形节点的配置项 -->
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
    >
      <el-table-column prop="id" label="ID" width="60"> </el-table-column>
      <el-table-column prop="goodsname" label="商品名称" width="180">
      </el-table-column>
      <el-table-column prop="firstcatename" label="一级分类名称" width="120">
      </el-table-column>
      <el-table-column prop="price" label="价格" width="100"> </el-table-column>
      <el-table-column width="120" label="商品图片">
        <template slot-scope="scope">
          <img v-if="scope.row.img" width="100" :src="host + scope.row.img" />
        </template>
      </el-table-column>
      <el-table-column width="80" label="是否新品">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isnew"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
            @change="changeNew(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column width="80" label="是否热卖">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.ishot"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="status" width="70" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" type="success">正常</el-tag>
          <el-tag v-else type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            @click="edit(scope.row)"
          ></el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="del(scope.row.uid)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 数据量比较大时 会考虑通过分页来按需加载数据 提高速度 -->
    <!-- 分页中的几个关键数据：
      1.总数量 total 数据在后台需要发请求
      2.每页有几条数据 size 这个数据在前端
      3.总页数 这个值根据前两个值计算得到 pages
      4.当前是第几页 currentPage
     -->
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-size="size"
      layout="total, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { getTotalGoods, getGoodsList, editGoods} from "../../apis";
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      size: 5,
      total: 0,
    };
  },
  created() {
    // 发请求获取数据总量
    this.getTotalList();
  },
  methods: {
    // 快捷操作 将弹窗中的操作提前到列表页完成
    async changeNew(data) {
      delete data.firstcatename
      delete data.secondcatename
      let fd = new FormData();
      for (const key in data) {
        fd.append(key, data[key])
      }
      let res = await editGoods(fd);
      console.log(res);
    },
    handleCurrentChange(val) {
      // 修改页码
      this.currentPage = val;
      // 获取页码对应的数据
      this.getFinalList();
    },
    async getTotalList() {
      let res = await getTotalGoods();
      this.total = res.list[0].total;
      // 先获取总数量之后 再 getUserList
      this.getFinalList();
    },
    async getFinalList() {
      let res = await getGoodsList({ size: this.size, page: this.currentPage });
      // console.log(res);
      this.tableData = res.list;
    },
    // 编辑--分两步1.获取老数据 2.提交新数据
    edit(data) {
      // 子传父 打开弹窗 展示这些数据
      this.$emit("edit", data);
    },
    // 删除
    del(uid) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        // 点击确定按钮执行这里
        .then(async () => {
          // 真的删除
          let res = await delMenu(uid);
          if (res.code === 200) {
            // 提示成功
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            // 更新数据
            this.tableData = res.list;
          }
        })
        // 点击取消按钮执行这里
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style>
</style>